﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Library.DomainObject.SQLServer;
using System.Collections.ObjectModel;
using Web.Library.Facade.SQLServer;
using System.Configuration;
using Web.Support;

public partial class Control_Menu : BaseUserControl
{
    public Collection<SysMenu> LeftMenu = new Collection<SysMenu>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.User.Identity.IsAuthenticated) topmenu.Visible = true;
        else topmenu.Visible = false;

        MenuInit();
        MenuActive();
    }

    private ITemplate headermenu = null;
    private ITemplate sidebarmenu = null;
    [TemplateContainer(typeof(TemplateControl))]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [TemplateInstance(TemplateInstance.Single)]
    public ITemplate HeaderMenu
    {
        set { headermenu = value; }
        get { return headermenu; }
    }

    public ITemplate SidebarMenu
    {
        set { sidebarmenu = value; }
        get { return sidebarmenu; }
    }

    protected void MenuInit()
    {
        try
        {
            int port = Request.Url.Port;
            string baseServer = Request.Url.Host + ":" + port.ToString();
            string url = "http://" + baseServer;
            string AppDomainAppVirtualPath = System.Web.HttpRuntime.AppDomainAppVirtualPath;
            //return url;

            //string domain = ConfigurationManager.AppSettings["DomainApp"].ToString();
            string domain = Request.Url.GetLeftPart(UriPartial.Authority) + "/";
            //MenuFacade menuFacade = new MenuFacade();
            SysMenuFacade menuFacade = new SysMenuFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

            if (AppDomainAppVirtualPath.Trim().Length >1)
                domain = url + AppDomainAppVirtualPath+ "/";
            else
                domain = url+"/";

            //AuthInfo authinfo = new AuthInfo().UserInfo();

            List<Dictionary<string, Dictionary<string, string>>> menu = new List<Dictionary<string, Dictionary<string, string>>>();

            SysGroupAuthorizationFacade Facade = new SysGroupAuthorizationFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

            menu = Facade.RetrieveMenuAuthorizationByGroupIDAndParentID(Convert.ToInt32(base.GroupID), 0, "", new List<Dictionary<string, Dictionary<string, string>>>(), false);
           
            //authinfo.MenuAuthorization = menu;


            if (headermenu != null && Page.User.Identity.IsAuthenticated)
            {
                headermenu.InstantiateIn(Menu1);
                Menu1.Visible = true;
                //if (authinfo.MenuAuthorization.Count > 0)
                if(menu.Count>0)
                {
                    string menu1 = string.Empty;
                    foreach (Dictionary<string, Dictionary<string, string>> obj in menu)// authinfo.MenuAuthorization)
                    {
                        if (obj["Menu"]["Parent"].ToString().IndexOf("_") == -1)
                        {
                            string[] urlp = obj["Menu"]["Url"].ToString().Split('/');
                            menu1 += "<li rel='" + urlp[0].Replace(".aspx", "") + "'><a href='" + domain + obj["Menu"]["Url"].ToString() + "'>" + obj["Menu"]["Title"].ToString() + " <span>▼</span></a></li>";
                        }
                    }
                    topmenu.InnerHtml = menu1;
                }
            }

            if (sidebarmenu != null)
            {
                sidebarmenu.InstantiateIn(Menu2);
                Menu2.Visible = true;

                string path = HttpContext.Current.Request.Url.AbsolutePath.Replace(domain, "");
                string[] MenuParent = path.Split('/');
                string Parent = string.Empty;
                if(AppDomainAppVirtualPath.Trim().Length>1)
                    Parent = MenuParent[2];
                else
                    Parent = MenuParent[1];

                //List<Dictionary<string, Dictionary<string, string>>> menu;
                //GroupAuthorizationFacade authorizationFacade = new GroupAuthorizationFacade();
                SysGroupAuthorizationFacade authorizationFacade = new SysGroupAuthorizationFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

                //if (authinfo.MenuAuthorization.Count > 0)
                if (menu.Count > 0)
                {
                    string menu2 = string.Empty;
                    foreach (Dictionary<string, Dictionary<string, string>> obj in menu)// authinfo.MenuAuthorization)
                    {
                        menu = new List<Dictionary<string, Dictionary<string, string>>>();
                        menu = authorizationFacade.RetrieveMenuAuthorizationByGroupIDAndParentID(base.GroupID, Convert.ToInt32(obj["Menu"]["ID"].ToString()), obj["Menu"]["Parent"].ToString(), new List<Dictionary<string, Dictionary<string, string>>>(), true);

                        foreach (Dictionary<string, Dictionary<string, string>> obj2 in menu)
                        {
                            if (obj2["Menu"]["Parent"].ToString().IndexOf(Parent + "_") > -1)
                            {
                                if (obj2["Menu"]["Title"] == "-") menu2 += "<li class='separator'></li>";
                                else
                                {
                                    string[] urlp = obj2["Menu"]["Url"].ToString().Split('/');
                                    menu2 += "<li rel='" + urlp[1].Replace(".aspx", "") + "'><a href='" + domain + obj2["Menu"]["Url"].ToString() + "'>" + obj2["Menu"]["Title"].ToString() + "</a></li>";
                                }
                            }
                        }
                    }
                    sidemenu.InnerHtml = menu2;
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
    }

    protected void MenuActive()
    {

    }
}